package com.tos_bot.utility;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
/**
* @author frankwang
*
*/
public class ImageHelper {
public ImageHelper(){
}
static public void savePng(String Path, Bitmap bm) {
try {
File file = new File(Path + ".png");
file.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
bm.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static public Bitmap resize(Bitmap bm, int newWidth, int newHeight){
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
public static int[] BitmaptoGrayscale(Bitmap bmSrc)
{
/*
int color[] = new int[6]; // RGBDLH
int picw = bmpOriginal.getWidth();
int pich = bmpOriginal.getHeight();
int[] pix = new int[picw * pich];
int[] ret = new int[picw * pich];
bmpOriginal.getPixels(pix, 0, picw, 0, 0, picw, pich);
int r, g, b;
for (int i = 0; i < pix.length; i++) {
int _red = ((pix[i]) >> 16) & 0xFF;
int _green = ((pix[i]) >> 8) & 0xFF;
int _blue = ((pix[i])) & 0xFF;
ret[i]=(int) (0.3 * _red + 0.59 * _green + 0.11 * _blue);
}
*/
int width = bmSrc.getWidth();
int height = bmSrc.getHeight();
// 创建目标灰度图像
Bitmap bmpGray = null;
bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
// 创建画布
Canvas c = new Canvas(bmpGray);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmSrc, 0, 0, paint);
int[] pix = new int[width * height];
int i =0;
for(int h =0;h<height;h++)
for(int w=0;w<width;w++){
pix[i] = bmpGray.getPixel(w,h);
i++;
}
return pix;
}
public static int average(int[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
public static int rgbToGray(int pixels) {
// int _alpha = (pixels >> 24) & 0xFF;
int _red = (pixels >> 16) & 0xFF;
int _green = (pixels >> 8) & 0xFF;
int _blue = (pixels) & 0xFF;
return (int) (0.3 * _red + 0.59 * _green + 0.11 * _blue);
}
public static char binaryToHex(int binary) {
char ch = ' ';
switch (binary)
{
case 0:
ch = '0';
break;
case 1:
ch = '1';
break;
case 2:
ch = '2';
break;
case 3:
ch = '3';
break;
case 4:
ch = '4';
break;
case 5:
ch = '5';
break;
case 6:
ch = '6';
break;
case 7:
ch = '7';
break;
case 8:
ch = '8';
break;
case 9:
ch = '9';
break;
case 10:
ch = 'a';
break;
case 11:
ch = 'b';
break;
case 12:
ch = 'c';
break;
case 13:
ch = 'd';
break;
case 14:
ch = 'e';
break;
case 15:
ch = 'f';
break;
default:
ch = ' ';
}
return ch;
}
}